﻿using System;
using System.Collections.Generic;
using System.Text;
using LearnNet.DAL;
using LearnNet.Entity;
using System.Data;
using System.Data.Common;
namespace LearnNet.BLL
{
    public class BLLHelper
    {
        #region 当前数据库类型
        /// <summary>
        /// 返回数据库类型字符串形式
        /// </summary>
        /// <returns></returns>
        public static string GetDBType()
        {
            return DALHelper.LearnNetHelper.DatabaseType.ToString();
        }
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="path">数据库备份地址</param>
        /// <returns></returns>
        public static Int32 BackUpData(string path)
        {
            return DALHelper.LearnNetHelper.ExecuteNonQuery(CommandType.Text, string.Concat("BACKUP DATABASE LearnNet TO DISK ='", path, "'"), null);
        }
        #endregion
        
        /// <summary>
        /// 返回查询的字段值
        /// </summary>
        /// <param name="sql">执行的语句</param>
        /// <returns>字符串类型</returns>
        public static string ExecuteScalar(string sql)
        {
            object obj = DALHelper.LearnNetHelper.ExecuteScalar(CommandType.Text, sql, null);
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }

        #region 构造sql语句
        /// <summary>
        /// 获取分页SQL
        /// </summary>
        /// <param name="qp">查询条件类</param>
        /// <returns></returns>
        public static string GetPagerSQL(QueryParam qp)
        {
            return DALHelper.LearnNetHelper.GetPagerSQL(qp);
        }

        /// <summary>
        /// 时间转sql语句
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetSqlDateTime(DateTime dt)
        {
            return DALHelper.LearnNetHelper.GetSqlDateTime(dt);
        }
        #endregion

        #region 更新字段方法
        /// <summary>
        /// 更新数据库字段
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="TableFiledsValue">更新字段和值</param>
        /// <param name="Where">更新条件</param>
        /// <returns></returns>
        public static int Update(string table, string TableFiledsValue, string Where)
        {
            return DALHelper.LearnNetHelper.ExecuteNonQuery(CommandType.Text, string.Concat("update ", table, " set ", TableFiledsValue, " where ", Where), null);

        }

        #endregion

        #region "ReportContent (发送内容 ) - Method"
        /// <summary>
        /// 新增/删除/修改 ReportContentEntity (发送内容)
        /// </summary>
        /// <param name="fam">ReportContentEntity实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public static Int32 ReportContentInsertUpdateDelete(ReportContentEntity fam)
        {
            return DALHelper.LearnNetHelper.ReportContentInsertUpdateDelete(fam);
        }
        /// <summary>
        /// 根据Id返回 ReportContentEntity实体类 单笔资料 (发送内容)
        /// </summary>
        /// <param name="Id">Id Id</param>
        /// <returns>返回 ReportContentEntity实体类 Id为0则无记录</returns>
        public static ReportContentEntity ReportContentDisp(Int32 Id)
        {
            ReportContentEntity fam = new ReportContentEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Format("{0}.{1} = {2}", "ReportContent", "Id", Id);
            int RecordCount = 0;
            List<ReportContentEntity> lst = ReportContentList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rContent"></param>
        /// <returns></returns>
        public static ReportContentEntity ReportContentDisp(string rContent)
        {
            ReportContentEntity fam = new ReportContentEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Format("{0}.{1} = '{2}'", "ReportContent", "sendContent", rContent);
            int RecordCount = 0;
            List<ReportContentEntity> lst = ReportContentList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 返回ReportContentEntity实体类的List对象 (发送内容)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>ReportContentEntity实体类的List对象(发送内容)</returns>
        public static List<ReportContentEntity> ReportContentList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = "ReportContent";
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "Id";
            }
            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            return DALHelper.LearnNetHelper.ReportContentList(qp, true, out RecordCount);
        }
        #endregion
 
        #region "ReportList (发送链接 ) - Method"
        /// <summary>
        /// 新增/删除/修改 ReportListEntity (发送链接)
        /// </summary>
        /// <param name="fam">ReportListEntity实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public static Int32 ReportListInsertUpdateDelete(ReportListEntity fam)
        {
            return DALHelper.LearnNetHelper.ReportListInsertUpdateDelete(fam);
        }
        /// <summary>
        /// 根据Id返回 ReportListEntity实体类 单笔资料 (发送链接)
        /// </summary>
        /// <param name="Id">Id 序号</param>
        /// <returns>返回 ReportListEntity实体类 Id为0则无记录</returns>
        public static ReportListEntity ReportListDisp(Int32 Id)
        {
            ReportListEntity fam = new ReportListEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Format("{0}.{1} = {2}", "ReportList", "Id", Id);
            int RecordCount = 0;
            List<ReportListEntity> lst = ReportListList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 根据Id返回 ReportListEntity实体类 单笔资料 (发送链接)
        /// </summary>
        /// <param name="Id">Id 序号</param>
        /// <returns>返回 ReportListEntity实体类 Id为0则无记录</returns>
        public static ReportListEntity ReportListDisp(string url)
        {
            ReportListEntity fam = new ReportListEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 2;
            qp.Where = string.Format("{0}.{1} like '%{2}%'", "ReportList", "url", url);
            int RecordCount = 0;
            List<ReportListEntity> lst = ReportListList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }


        /// <summary>
        /// 返回ReportListEntity实体类的List对象 (发送链接)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>ReportListEntity实体类的List对象(发送链接)</returns>
        public static List<ReportListEntity> ReportListList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = "ReportList";
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "Id";
            }
            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            return DALHelper.LearnNetHelper.ReportListList(qp, true, out RecordCount);
        }
        #endregion
        #region "SiteInfo (网站列表 ) - Method"
        /// <summary>
        /// 新增/删除/修改 SiteInfoEntity (网站列表)
        /// </summary>
        /// <param name="fam">SiteInfoEntity实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public static Int32 SiteInfoInsertUpdateDelete(SiteInfoEntity fam)
        {
            return DALHelper.LearnNetHelper.SiteInfoInsertUpdateDelete(fam);
        }
        /// <summary>
        /// 根据Id返回 SiteInfoEntity实体类 单笔资料 (网站列表)
        /// </summary>
        /// <param name="Id">Id 编号</param>
        /// <returns>返回 SiteInfoEntity实体类 Id为0则无记录</returns>
        public static SiteInfoEntity SiteInfoDisp(Int32 Id)
        {
            SiteInfoEntity fam = new SiteInfoEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Format("{0}.{1} = {2}", "SiteInfo", "Id", Id);
            int RecordCount = 0;
            List<SiteInfoEntity> lst = SiteInfoList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 根据Id返回 SiteInfoEntity实体类 单笔资料 (网站列表)
        /// </summary>
        /// <param name="Id">Id 编号</param>
        /// <returns>返回 SiteInfoEntity实体类 Id为0则无记录</returns>
        public static SiteInfoEntity SiteInfoDisp(string  url)
        {
            SiteInfoEntity fam = new SiteInfoEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Format("{0}.{1} = '{2}'", "SiteInfo", "siteUrl", url);
            int RecordCount = 0;
            List<SiteInfoEntity> lst = SiteInfoList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 返回SiteInfoEntity实体类的List对象 (网站列表)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>SiteInfoEntity实体类的List对象(网站列表)</returns>
        public static List<SiteInfoEntity> SiteInfoList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = "SiteInfo";
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "Id";
            }
            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            return DALHelper.LearnNetHelper.SiteInfoList(qp, true, out RecordCount);
        }
        #endregion
        #region "UserInfo (用户信息 ) - Method"
        /// <summary>
        /// 新增/删除/修改 UserInfoEntity (用户信息)
        /// </summary>
        /// <param name="fam">UserInfoEntity实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public static Int32 UserInfoInsertUpdateDelete(UserInfoEntity fam)
        {
            return DALHelper.LearnNetHelper.UserInfoInsertUpdateDelete(fam);
        }
        /// <summary>
        /// 根据ID返回 UserInfoEntity实体类 单笔资料 (用户信息)
        /// </summary>
        /// <param name="ID">ID </param>
        /// <returns>返回 UserInfoEntity实体类 ID为0则无记录</returns>
        public static UserInfoEntity UserInfoDisp(Int32 ID)
        {
            UserInfoEntity fam = new UserInfoEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Format("{0}.{1} = {2}", "UserInfo", "ID", ID);
            int RecordCount = 0;
            List<UserInfoEntity> lst = UserInfoList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 返回 UserInfoEntity实体类 单笔资料 (用户信息)
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static UserInfoEntity UserInfoDisp(string name ,string pwd,string type)
        {
            UserInfoEntity fam = new UserInfoEntity();
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where = string.Concat("name='", name, "' and pwd='" + pwd + "' and type='" + type + "' ");
            int RecordCount = 0;
            List<UserInfoEntity> lst = UserInfoList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 返回UserInfoEntity实体类的List对象 (用户信息)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>UserInfoEntity实体类的List对象(用户信息)</returns>
        public static List<UserInfoEntity> UserInfoList(QueryParam qp, out int RecordCount)
        {
            qp.TableName = "UserInfo";
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "ID";
            }
            if (qp.ReturnFields == null)
            {
                qp.ReturnFields = "*";
            }
            return DALHelper.LearnNetHelper.UserInfoList(qp, true, out RecordCount);
        }
        #endregion
 







    }
}
